# coding: utf-8
# Author: Legend奇
# @Time： 2020/7/21 下午3:51

from gettext import gettext as _

from tortoise import fields

from base.base_model import MyAbstractBaseModel
from base.choices import STAT_FIELDS_DATA_TYPE, StatFieldMergeStrategy, STAT_FIELDS_MERGE_STRATEGY
from base.constant import DataType


class StatConfig(MyAbstractBaseModel):
    device_info_collect_interval = fields.IntField(default=60)
    device_run_collect_interval = fields.IntField(default=60)
    service_run_collect_interval = fields.IntField(default=60)
    service_business_collect_interval = fields.IntField(default=60)
    preservation_days = fields.IntField(default=60)

    class Meta:
        table = 'statconfig'


class StatField(MyAbstractBaseModel):
    name = fields.CharField(max_length=64, default='', verbose_name=_('statistic field name'))
    metric = fields.CharField(max_length=255, default='', blank=False, verbose_name=_('metric name'))
    field_name = fields.CharField(max_length=64, default='', verbose_name=_('statistic field name'))
    is_tag = fields.SmallIntField(default=0)
    data_type = fields.CharField(max_length=32, default=DataType.STR, choices=STAT_FIELDS_DATA_TYPE)
    stat_type = fields.IntField(default=0, verbose_name=_('statistic type'))
    merge_strategy = fields.IntField(default=StatFieldMergeStrategy.NULL, choices=STAT_FIELDS_MERGE_STRATEGY)
    stat_id = fields.IntField(null=True, verbose_name=_('statistic field id'))
    alarm_enable = fields.SmallIntField(default=0)
    measurement = fields.CharField(default='', max_length=128)
    app_id = fields.IntField(null=True)
    datasource_object_id = fields.IntField(default=None, null=True)
    troubleshoot = fields.TextField(default='', verbose_name=_('troubleshoot'))
    pipe_type = fields.CharField(null=True, default='', max_length=128, verbose_name=_('pipe type'))

    class Meta:
        db_table = 'statfield'
        verbose_name = _('statistic field')


class StatType(MyAbstractBaseModel):
    name = fields.CharField(default='', max_length=128)
    code = fields.IntField(default=0)
    datasource_id = fields.IntField(default=0)

    class Meta:
        table = 'stattype'


class DataSourceType(MyAbstractBaseModel):
    name = fields.CharField(default='', max_length=128)
    table = fields.CharField(default='', max_length=128)
    model = fields.CharField(default='', max_length=128)

    class Meta:
        table = 'datasourcetype'
